﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MathGraphics;

namespace MathGraphics
{
    public static class GraphicsMethod
    {
        /// <summary>
        /// 判断圆与矩形是否相交
        /// </summary>
        /// <param name="rec">矩形</param>
        /// <param name="ptCirCenter">圆心</param>
        /// <param name="radius">半径</param>
        public static bool RectangeAndCircleIntersect(RectangleF rec, Circle circle)
        {
            PointF ptRecCenter = new PointF(
                (rec.Left + rec.Right) / 2, (rec.Top + rec.Bottom) / 2); //求矩形中心
            Vector2F vRec2Cir = new Vector2F(Math.Abs(circle.X - ptRecCenter.X), Math.Abs(circle.Y - ptRecCenter.Y)); //求矩形到圆心的向量并转换至第一象限
            Vector2F vRecHalfLen = new Vector2F(rec.Right - ptRecCenter.X, rec.Bottom - ptRecCenter.Y); //矩形半长向量（第一象限）
            Vector2F vDiff = vRec2Cir - vRecHalfLen;
            if (vRec2Cir.X > vRecHalfLen.X)
                if (vRec2Cir.Y < vRecHalfLen.Y)
                    return vDiff.X < circle.Radius;
                else
                    return (vDiff.X * vDiff.X + vDiff.Y * vDiff.Y) < circle.Radius * circle.Radius;
            else
                if (vRec2Cir.Y < vRecHalfLen.Y)
                    return true;
                else
                    return vDiff.Y < circle.Radius;
        }
    }
}
